Skip to content

Add support for Optional parameters#7

Merged
anttip merged 1 commit intomasterfrom
improvement/add-optional-support
Mar 14, 2025
Merged

Add support for Optional parameters#7
anttip merged 1 commit intomasterfrom
improvement/add-optional-support

Conversation

@anttip
Copy link

@anttip anttip commented Mar 13, 2025

Adds support for binding to java.util.Optional parameters. Binding to Optional parameters allows implementing partial updates. With a partial update the mutate call can update only some of the Optional fields without needing to repeat all the existing values of an object.

The mapping from an optional field in GraphQL schema that binds to java.util.Optional is the following:

  1. value given to the field in GraphQL call is mapped into java.util.Optional of the underlying type
  2. null given to the field in GraphQL call is mapped into Optional.empty to signal the removal of a possibly existing value of the field
  3. field not included in the GraphQL call is mapped into null to signal undefined so any possibly existing value(s) should not be modified

}
return val;
} else {
return arg;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in which cases will this be possible?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built-in types, like String and similar

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaaah, "native" types for which there isn't ParameterizedType information, got it

if (arg == null) {
// for Optional parameters null should be returned as Optional.empty() to show a request for a null value
// and not including the parameter in the query at all should be returned as null to show "undefined" value / not set
if ((p instanceof ParameterizedType && ((ParameterizedType) p).getRawType() == Optional.class)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, it's assumed lines 141-147 will be executed before this in the recursion? and thus adding the "undefined" nulls there?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. Alternative could be to change arg-parameter from Object to use Optional or similar so the missing values could be represented through that, but I didn't explore that implementation.

Copy link

@leanish leanish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added 2 questions, but LGTM if the answer is "yes"

@anttip anttip merged commit 66fc6c1 into master Mar 14, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants